//
//  ScreeningView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/27.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "ScreeningView.h"
#import "ScreeningCell.h"
@interface ScreeningView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end


@implementation ScreeningView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self createAndLayoutCollectionView];
    }
    return self;
}

-(void)createAndLayoutCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(scaleLeftAndRight(75), scaleTopAndBottom(30));
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    self.collectionView.allowsMultipleSelection = YES;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[ScreeningCell class] forCellWithReuseIdentifier:@"ScreeningCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }
    return 8;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ScreeningCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ScreeningCell" forIndexPath:indexPath];
    cell.label.font = [UIFont systemFontOfSize:13];
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section == 0) {
           [self creatSubviewsInView:view WithTitle:@"猜你喜欢"];
        }
        if (indexPath.section == 1) {
            [self creatSubviewsInView:view WithTitle:@"更多服务"];
        }
        return view;
    }else{
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            [self createLineView:view];
        }else{
            view.backgroundColor = [UIColor clearColor];
           [self createFooterView:view];
        }
        
        return view;
    }
   
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(20.0f, 20.0f);
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(40));
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, scaleTopAndBottom(30));
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ScreeningCell *cell = (ScreeningCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.label.layer.borderColor = COLOR_HEX(0xF8B50C).CGColor;
    cell.label.backgroundColor = COLOR_HEX(0xFDF5DF);
    cell.label.textColor = COLOR_HEX(0xF8B50C);
    
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    ScreeningCell *cell = (ScreeningCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.label.layer.borderColor = COLOR_HEX(0xDCDCDC).CGColor;
    cell.label.textColor = [UIColor blackColor];

}
/**
 创建头部视图的view

 */
-(void)creatSubviewsInView:(UICollectionReusableView *)view WithTitle:(NSString *)title{
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    [view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(15);
        make.centerY.equalTo(view.mas_centerY);
        make.right.equalTo(view.mas_right).offset(-15);
        make.height.mas_equalTo(scaleTopAndBottom(30));
    }];
}

/**
 创建尾部视图
 */
-(void)createFooterView:(UICollectionReusableView *)view{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(0);
        make.right.equalTo(view.mas_right).offset(0);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(view.mas_top).offset(5);
    }];
    UIButton *deleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleBtn.backgroundColor = [UIColor whiteColor];
    deleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [deleBtn setTitleColor:COLOR_HEX(0x333333) forState:UIControlStateNormal];
    [deleBtn setTitle:@"清空筛选" forState:UIControlStateNormal];
    [view addSubview:deleBtn];
    UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    finishBtn.backgroundColor = COLOR_HEX(0xF8B50C);
        [finishBtn setTitleColor:COLOR_HEX(0x333333) forState:UIControlStateNormal];
    finishBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [view addSubview:finishBtn];
    [deleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.equalTo(view);
        make.top.equalTo(lineView.mas_bottom).offset(0);
    }];
    [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(view);
        make.left.equalTo(deleBtn.mas_right);
        make.top.equalTo(lineView.mas_bottom).offset(0);
        make.width.equalTo(deleBtn);
    }];
    [finishBtn addTarget:self action:@selector(sendValues:) forControlEvents:UIControlEventTouchUpInside];
    [deleBtn addTarget:self action:@selector(deleAllChoose:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)deleAllChoose:(UIButton *)btn{
    [self.collectionView removeFromSuperview];
    [self createAndLayoutCollectionView];
    [self.collectionView reloadData];
}
-(void)sendValues:(UIButton *)btn{
    if (self.sendAllValue) {
        self.sendAllValue(@"1");
    }
}
-(void)createLineView:(UICollectionReusableView *)view{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(10);
        make.right.equalTo(view.mas_right).offset(-10);
        make.height.mas_equalTo(0.5);
        make.centerY.equalTo(view.mas_centerY);
    }];
}

@end
